package cn.javase.concurrent._设计模式._异步模式之生产者消费者;

import java.util.List;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Test {
    public static void main(String[] args) {
        MessageQueue queue = new MessageQueue(1);
        for (int i = 0; i < 4; i++) {
            int id = i;
            new Thread(() -> {
                try {
                    Thread.sleep(2000);
                    queue.put(new Message(id, "结果" + id));
                    log.debug("put message {}", id);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }, "生产者" + i).start();
        }
        // 1 个消费者线程, 处理结果
        new Thread(() -> {
            while (true) {
                Message message = queue.take();
                log.debug("take message({}): {}", message.getId(), message.getMessage());
            }
        }, "消费者").start();
    }
}
